package com.example.swim.controller;


import com.example.swim.Pojo.*;
import com.example.swim.Service.AcService;
import com.example.swim.Service.CardService;
import com.example.swim.Service.NewsService;
import com.example.swim.Service.VipService;
import com.example.swim.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author LiYa
 * @create 2022-07-30 11:02
 */
@RestController
@RequestMapping("/vip")
public class VipController {
    @Autowired
    VipService vipService;
    @Autowired
    AcService acService;
    @Autowired
    CardService cardService;
    @Autowired
    NewsService newsService;

    @RequestMapping("/toMain")
    public R mian(VIP loginForm)
    {
        VIP vip;
        HashMap<String,Object> map=new HashMap<>();
        if ((vip=vipService.findVIP(loginForm))!=null)
        {
            map.put("user", vip);
            map.put("identity", "vip");
            return new R(true,map);
        }
       return  new R(false, "会员密码或账号错误，请重新输入");
    }
    @RequestMapping("/main")
    public R toMain(VIP vip)
    {
//        将持久类内容与数据库同步
        vip=vipService.findVIP(vip);
        HashMap<String,Object> map=new HashMap<>();
        List<News> news=newsService.getAll();
        map.put("vip", vip);
        map.put("news", news);
        map.put("card", cardService.findById(vip.getGrade()));
        if(vip.getAid()!=null)
        {
            Activity ac=acService.findAc(vip.getAid());
            map.put("ac", ac);
        }
        return  new R(true,map);
    }
//    提交注册信息
    @RequestMapping("/apply")
    public R Register(VIP vip)
    {
        long id=vipService.insertVIP(vip);
       return new R(true, "注册成功啦"+'\n'+"账号id为:"+id+'\n'+"密码为:"+vip.getPassword());
    }
    // 退选活动
    @PostMapping("/quitA")
    public R quit1(VIP vip){

        VIP  v=vipService.quitA(vip);
        HashMap<String,Object> map=new HashMap<>();
        map.put("user",v);
        map.put("identity","vip");
        return  new R(true, map);
    }

//    修改个人信息
    @PostMapping("/updateVip")
    public R update(VIP vip){
        vipService.update(vip);
        return new R(true,"修改成功");
    }

}
